package photos;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import dbTools.DBTools;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;
import java.io.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * Servlet implementation class PhotoAddSelect
 */
@WebServlet("/PhotoAddSelect")
public class PhotoAddSelect extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PhotoAddSelect() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=GBK");
		request.setCharacterEncoding("GBK");
		response.setCharacterEncoding("GBK");
		String s=request.getParameter("id");
		String[] ss=s.split(",");
		String id=ss[0];
		String sql1="SELECT * FROM photoInfo WHERE  photoId="+id+";";
		DBTools db=new DBTools();
		db.open();
		PhotoInfoMessageBean sc=null;
		ArrayList al1=new ArrayList();
		ResultSet rs1=db.select(sql1);
		try {
			while (rs1.next()) {
				sc=new PhotoInfoMessageBean(rs1.getInt(1),rs1.getString(2));
				al1.add(sc);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.setAttribute("sm", sc);
		request.setAttribute("i", ss[1]);
		db.close();
		request.getRequestDispatcher("/updatePhoto.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		SmartUpload su = new SmartUpload();
		PageContext page = JspFactory.getDefaultFactory().getPageContext(this, request, response, null, true, 1024, true);
		su.initialize(page);
		try {
			su.upload();
			int count = su.save("/img/goodsPhotos");
			Files fs = su.getFiles();
			for (int k = 0; k < fs.getCount(); k++) {
				File f = fs.getFile(k);
				String photoName = f.getFileName();
				ServletContext application = super.getServletContext();
				String path = application.getRealPath("");
				String src=path+"\\img\\goodsPhotos\\"+photoName;
				InputStream is = new FileInputStream(path+"\\img\\goodsPhotos\\"+photoName);
				int s = is.available();//�õ��ļ���С
				byte data[] = new byte[s];
				is.read(data);//������
				OutputStream os = new FileOutputStream(path+"\\img\\goodsPhotos\\"+photoName);
				os.write(data);
				BufferedInputStream jd = new BufferedInputStream(is);
				BufferedOutputStream je = new BufferedOutputStream(os);
				int content = 0;
				while ((content = jd.read())!=-1) {
					je.write(content);
				}
				DBTools db=new DBTools();
				db.open();
				String sql = "INSERT  INTO photoInfo(photoSrc) VALUES('"+photoName+"')";
				int r=db.update(sql);
				db.close();
				os.close();
				is.close();
				
			}
			if (count > 0) {
				int i=1;
				int i2=0;
				String sql1="SELECT * FROM photoInfo";
				DBTools db=new DBTools();
				db.open();
				PhotoInfoMessageBean sc=null;
				ArrayList al1=new ArrayList();
				ResultSet rs1=db.select(sql1);
				try {
					while (rs1.next()) {
						sc=new PhotoInfoMessageBean(rs1.getInt(1),rs1.getString(2));
						al1.add(sc);
					}
					if(al1.size()%5!=0){
						i2=al1.size()/5+1;
						if(i<1){
							i=al1.size()/5+1;
						}
						if(i>al1.size()/5+1){
							i=1;
						}	
					}else{
						i2=al1.size()/5;
						if(i<1){
							i=al1.size()/5;
						}
						if(i>al1.size()/5){
							i=1;
						}	
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				String sql2="SELECT * FROM photoInfo ORDER BY photoId DESC LIMIT "+(i-1)*5+",5;";
				ResultSet rs2=db.select(sql2);
				ArrayList al2=new ArrayList();
				try {
					while (rs2.next()) {
						sc=new PhotoInfoMessageBean(rs2.getInt(1),rs2.getString(2));
						al2.add(sc);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				request.setAttribute("i", i);
				request.setAttribute("i2", i2);
				request.setAttribute("al", al2);
				db.close();
				request.getRequestDispatcher("/photo.jsp").forward(request, response);
				
			}
		} catch (SmartUploadException e) {
			// TODO: handle exception
		}
	}
	

}
